﻿using System.Windows.Forms;
using Home.Haushalt.Lib;
using System.Data;
using Home.Haushalt.Lib.Element;
using System;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win;
using System.IO;
using System.Drawing;

namespace Home.Haushalt.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmLayoutName : Form
    {
        /// <summary></summary>
        public string LayoutName { get { return txtLayoutName.Text; } set { txtLayoutName.Text = value; } }

        /// <summary></summary>
        public string LayoutIcon { get { return txtIcon.Text.Trim(); } set { txtIcon.Text = value; } }

        /// <summary></summary>
        public bool IsStartFavorit { get { return cbFavorit.Checked; } set { cbFavorit.Checked = value; } }

        /// <summary>
        /// 
        /// </summary>
        public FrmLayoutName()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="layoutName"></param>
        /// <param name="existsGridLayout"></param>
        public FrmLayoutName(Type elementType, string layoutName, GridLayout existsGridLayout)
        {
            InitializeComponent();

            //Icon-Combobox initialisieren
            txtIcon.DataSource = DAO.ViewIconTable;
            txtIcon.ValueMember = DAO.COLUMNNAME_VIEWICON_NAME;
            txtIcon.DisplayMember = DAO.COLUMNNAME_VIEWICON_NAME;
            txtIcon.DisplayLayout.Bands[0].ColHeadersVisible = false;

            EditorButton btnSelectIconFile = new EditorButton("open");
            btnSelectIconFile.Click += new EditorButtonEventHandler(btnSelectIconFile_Click);
            btnSelectIconFile.ButtonStyle = UIElementButtonStyle.Office2007RibbonButton;
            btnSelectIconFile.Width = 16;
            btnSelectIconFile.Appearance.Image = global:: Home.Haushalt.Lib.Properties.Resources.folderopen;
            txtIcon.ButtonsRight.Add(btnSelectIconFile);

            openViewIconFileDialog.InitialDirectory = DAO.HFile.IconsFullPath;

            //Default-Werte setzen
            DefaultColumnSets result = DefaultColumnSets.TabDefault;
            if (Enum.TryParse<DefaultColumnSets>(layoutName, out result))
            {
                LayoutName = "";
                LayoutIcon = "";
            }
            else
            {
                LayoutName = layoutName;
                LayoutIcon = existsGridLayout != null ? existsGridLayout.Icon : "";
                IsStartFavorit = existsGridLayout != null ? existsGridLayout.IsStartFavorit : false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectIconFile_Click(object sender, EditorButtonEventArgs e)
        {
            if (openViewIconFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtIcon.Text = openViewIconFileDialog.FileName;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtIcon_TextChanged(object sender, EventArgs e)
        {
            string iconFileName = txtIcon.Text;

            if (!iconFileName.Equals(string.Empty))
            {
                if (Path.GetDirectoryName(txtIcon.Text).Equals(string.Empty))
                {
                    iconFileName = Path.Combine(DAO.HFile.IconsFullPath, txtIcon.Text);
                }

                try
                {
                    imgIcon.Image = File.Exists(iconFileName) ? Image.FromFile(iconFileName) : null;
                }
                catch { }
            }
        }

    }
}
